package com.biShe.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.OrderVO;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.biShe.config.AliPayConfig;
import com.biShe.entity.Orders;
import com.biShe.mapper.OrdersMapper;
import com.biShe.vo.MyOrderVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/payment")
public class AlipayController{
    @Autowired
    private OrdersMapper ordersService;

    @SneakyThrows
    @RequestMapping("/pay")
    @ResponseBody

//    手机版的支付宝支付跳转（会跳转到app）
    public void aliPay() {
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.merchant_private_key, "json", AliPayConfig.charset, AliPayConfig.alipay_public_key, AliPayConfig.sign_type);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setNotifyUrl(AliPayConfig.notify_url);
        request.setReturnUrl(AliPayConfig.return_url);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no","111111");
        bizContent.put("total_amount", "0,01");
        bizContent.put("subject", "a");
        bizContent.put("product_code", "QUICK_WAP_WAY");
        bizContent.put("time_expire", LocalDateTime.now().plusMinutes(30));
//
        request.setBizContent(bizContent.toString());
//        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        String result = null;
        try {
            result = alipayClient.pageExecute(request).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        log.info("返回结果={}",result);
    }

//    Web版的支付宝扫码支付
    @SneakyThrows
    @RequestMapping("/payWeb")
    @ResponseBody
    public void aliPay2() {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AliPayConfig.gatewayUrl, AliPayConfig.app_id, AliPayConfig.merchant_private_key, "json", AliPayConfig.charset, AliPayConfig.alipay_public_key, AliPayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        //商户订单号，，必填
        String order_number = new String("1111123");
        //付款金额，从前台获取，必填
        String total_amount = new String("201314");
        //订单名称，必填
        String subject = new String("NewBoy");
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(aliPayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //输出
        log.info("返回结果={}",result);
    }


////    支付成功的回调函数
//    @SneakyThrows
//    @RequestMapping(value = "/notify",method = {RequestMethod.GET,RequestMethod.HEAD,RequestMethod.POST})  // 注意这里必须是POST接口
//    @ResponseBody
//    public void payNotify() {
//        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
//            System.out.println("=========支付宝异步回调========");
//
//            Map<String, String> params = new HashMap<>();
//            Map<String, String[]> requestParams = request.getParameterMap();
//            for (String name : requestParams.keySet()) {
//                params.put(name, request.getParameter(name));
//                // System.out.println(name + " = " + request.getParameter(name));
//            }
//            String tradeNo = params.get("out_trade_no");
//            String gmtPayment = params.get("gmt_payment");
//            String alipayTradeNo = params.get("trade_no");
//            System.out.println("交易名称: " + params.get("subject"));
//            System.out.println("交易状态: " + params.get("trade_status"));
//            System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
//            System.out.println("商户订单号: " + params.get("out_trade_no"));
//            System.out.println("交易金额: " + params.get("total_amount"));
//            System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
//            System.out.println("买家付款时间: " + params.get("gmt_payment"));
//            System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
//            // 更新订单为已支付(4)
//        }
//    }
}

